//
//  GYMineOrderEditVC.m
//  GYDemo
//
//  Created by mac on 2018/4/12.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineOrderEditVC.h"
#import "Head.h"

@interface GYMineOrderEditVC ()
@property (nonatomic,strong) UIView *confirmV;
@property (nonatomic,strong) UIButton *confirmBtn;
@property (nonatomic,strong) NSString *prices;
@property (nonatomic,strong) NSString *counts;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation GYMineOrderEditVC
#pragma mark - getter and setter
-(UIView *)confirmV
{
    if (!_confirmV) {
        _confirmV = [UIView new];
        _confirmV.width = self.view.width;
        _confirmV.height = WFCGFloatY(45);
        _confirmV.y = WF_UI_VIEW_HEIGHT - self.bottomShelterHeight - _confirmV.height;
        _confirmV.backgroundColor = Main_Color_White;
        
        [_confirmV addSubview:self.confirmBtn];
    }
    return _confirmV;
}

-(UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton new];
        _confirmBtn.width = WFCGFloatX(83.5);
        _confirmBtn.height = WFCGFloatY(25);
        _confirmBtn.x = self.view.width - WFCGFloatX(13 + 83.5);
        _confirmBtn.y = WFCGFloatY(11);
        [WFFunctions WFUIaddbordertoView:_confirmBtn radius:_confirmBtn.height / 2 width:0.7f color:Main_Color_Black];
        _confirmBtn.titleLabel.font = SYSTEMFONT(14);
        [_confirmBtn setTitleColor:Main_Color_Black forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmBtnTap:) forControlEvents:UIControlEventTouchUpInside];
        [_confirmBtn setTitle:@"保存" forState:UIControlStateNormal];
    }
    return _confirmBtn;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    [_dataSource removeAllObjects];
    [_dataSource addObjectsFromArray:_viewModel.orderInfoDetailModel.productList];
    return _dataSource;
}
#pragma mark - event
-(void)confirmBtnTap:(id)sender
{
    NSMutableDictionary *para = [NSMutableDictionary new];
    NSString *orderid = [NSString stringWithFormat:@"%@",self.viewModel.orderInfoDetailModel.idField];
    [para setObject:orderid forKey:@"orderid"];
    //[{"price":"300","count":"3","id":"25"}]
    NSMutableArray *marr = [NSMutableArray new];
    for (GYMineOrderInfoDetailGoodsModel *model in self.dataSource) {
        NSString *idd = [NSString stringWithFormat:@"%ld",model.idField];
        NSString *price = [NSString stringWithFormat:@"%ld",model.price];
        NSString *count = [NSString stringWithFormat:@"%ld",model.count];
        NSDictionary *dic = @{@"price":price,@"count":count,@"id":idd};
        [marr addObject:dic];
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:marr options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [para setObject:jsonString forKey:@"editlist"];
    [self.viewModel requestSave:para];
}
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"我的销售订单";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    self.isGroup = YES;
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = WF_UI_VIEW_HEIGHT - self.bottomShelterHeight - self.confirmV.height;
    
    [self.view addSubview:self.confirmV];
    [self viewModelBand];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.viewModel;
    observation.keyPath = @"orderInfoDetailModel";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation];
    
    GCBaseObservationModel *observation2 = [GCBaseObservationModel new];
    observation2.observation = self.viewModel;
    observation2.keyPath = @"SecUpdateSign";
    observation2.handler = ^(NSString *keyPath) {
        [selfp.viewModel requestDetail];
        [selfp.navigationController popViewControllerAnimated:YES];
    };
    [self registObservation:observation2];
}
-(void)reloadView
{
    [self.baseTable reloadData];
}
-(void)copyOrderTap:(id)sender
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.viewModel.orderInfoDetailModel.idField;
    [FTIndicator showToastMessage:@"复制成功!"];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineOrderInfoDetailGoodsModel *model = self.dataSource[indexPath.row];
    
    GYMineOrderEditCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineOrderEditCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setOrderListModel:model];
    cell.callback = ^(NSString *content, NSInteger type) {
        if (1 == type) {
            //price
            model.price = content.integerValue;
        }else{
            //num
            model.count = content.integerValue;
        }
        [self.dataSource replaceObjectAtIndex:indexPath.row withObject:model];
    };
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [GYMakeOrderCell finalHeight];
    return height;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *view = [UIImageView new];
    view.width = self.view.width;
    view.height = WFCGFloatY(45);
    view.backgroundColor = Main_Color_White;
    view.userInteractionEnabled = YES;
    
    UIView *sign = [UIView new];
    sign.backgroundColor = Main_Color_Red;
    sign.frame = WFCGRectMake(13, 16.5, 5, 15);
    sign.centerY = view.centerY;
    [view addSubview:sign];
    
    UILabel *titlelb = [UILabel new];
    titlelb.x = WFCGFloatX(23);
    titlelb.width = self.view.width - WFCGFloatX(26);
    titlelb.height = WFCGFloatY(34);
    titlelb.centerY = view.centerY;
    titlelb.font = SYSTEMFONT(15);
    titlelb.textColor = Main_Color_Black;
    GYMineOrderInfoDetailModel *info = self.viewModel.orderInfoDetailModel;
    titlelb.text = info.companyname;
    [view addSubview:titlelb];
    
    UIButton *phonenumbtn = [UIButton new];
    phonenumbtn.x = WFCGFloatX(234);
    phonenumbtn.width = WFCGFloatX(100);
    phonenumbtn.height = WFCGFloatX(16.5);
    phonenumbtn.centerY = view.centerY;
    phonenumbtn.titleLabel.font = SYSTEMFONT(12);
    [phonenumbtn setTitle:info.mobile forState:UIControlStateNormal];
    [phonenumbtn setTitleColor:Main_Color_Gray forState:UIControlStateNormal];
    [phonenumbtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [[GCPresenter shareInterface]phoneCall:info.mobile];
    }];
    [phonenumbtn setEnlargeEdge:10];
    [view addSubview:phonenumbtn];
    
    UIButton *phonesign = [UIButton new];
    phonesign.x = WFCGFloatX(340);
    phonesign.width = WFCGFloatX(14.5);
    phonesign.height = WFCGFloatX(14.5);
    phonesign.centerY = view.centerY;
    [phonesign setBackgroundImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
    //phonesign.backgroundColor = Main_Color_Blue;
    [phonesign handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [[GCPresenter shareInterface]phoneCall:info.mobile];
    }];
    [phonesign setEnlargeEdge:10];
    [view addSubview:phonesign];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return WFCGFloatY(44.5);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.width = self.view.width;
    view.height = WFCGFloatY(53 + 132.5);
    view.backgroundColor = Main_Color_White;
    
    UILabel *statelb = [UILabel new];
    statelb.x = WFCGFloatX(13);
    statelb.y = WFCGFloatY(12.5);
    statelb.width = WFCGFloatX(126);
    statelb.height = WFCGFloatY(20);
    statelb.font = SYSTEMFONT(14);
    statelb.textColor = Main_Color_Yellow;
    GYMineOrderInfoDetailModel *info = self.viewModel.orderInfoDetailModel;
    statelb.text = info.statusName;
    [view addSubview:statelb];
    
    UILabel *totallb = [UILabel new];
    totallb.x = self.view.width - WFCGFloatX(200 + 13);
    totallb.width = WFCGFloatX(200);
    totallb.y = WFCGFloatY(12.5);
    totallb.height = WFCGFloatY(20);
    totallb.font = SYSTEMFONT(14);
    totallb.textColor = Main_Color_Black;
    totallb.textAlignment = NSTextAlignmentRight;
    totallb.text = [NSString stringWithFormat:@"总价：%ld元",info.amount];
    [view addSubview:totallb];
    
    UIView *sep = [UIView new];
    sep.width = self.view.width;
    sep.height = 5.0f;
    sep.backgroundColor = Main_Color_BG;
    sep.y = view.height - sep.height;
    [view addSubview:sep];
    
    UILabel *ordernumlb = [UILabel new];
    ordernumlb.x = WFCGFloatX(13);
    ordernumlb.y = WFCGFloatY(53 + 19);
    ordernumlb.width = WFCGFloatX(160);
    ordernumlb.height = WFCGFloatY(16.5);
    ordernumlb.font = SYSTEMFONT(12);
    ordernumlb.textColor = Main_Color_Gray;
    NSString *ordernums = [NSString stringWithFormat:@"订单号：%@",info.idField];
    ordernumlb.text = ordernums;
    [view addSubview:ordernumlb];
    
    UIButton *copybtn = [UIButton new];
    copybtn.frame = WFCGRectMake(193, 53 + 17.5, 38, 18);
    copybtn.backgroundColor = Main_Color_LightGray;
    [WFFunctions WFUIaddbordertoView:copybtn radius:WFCGFloatY(2) width:0 color:nil];
    copybtn.titleLabel.font = SYSTEMFONT(11);
    [copybtn setTitle:@"复制" forState:UIControlStateNormal];
    [copybtn setTitleColor:Main_Color_White forState:UIControlStateNormal];
    [copybtn addTarget:self action:@selector(copyOrderTap:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:copybtn];
    
    UILabel *datelb = [UILabel new];
    datelb.x = WFCGFloatX(13);
    datelb.y = WFCGFloatY(53 + 54.5);
    datelb.width = self.view.width - WFCGFloatX(26);
    datelb.height = WFCGFloatY(16.5);
    datelb.font = SYSTEMFONT(12);
    datelb.textColor = Main_Color_Gray;
    NSString *dates = [NSString stringWithFormat:@"下单时间：%@",info.createtime];
    datelb.text = dates;
    [view addSubview:datelb];
    
    UILabel *commentlb = [UILabel new];
    commentlb.x = WFCGFloatX(13);
    commentlb.y = WFCGFloatY(53 + 90);
    commentlb.width = self.view.width - WFCGFloatX(26);
    commentlb.height = WFCGFloatY(16.5);
    commentlb.font = SYSTEMFONT(12);
    commentlb.textColor = Main_Color_Gray;
    NSString *comments = [NSString stringWithFormat:@"给买家留言：%@",info.descriptionField];
    //    commentlb.height = [WFFunctions WFStrGetSize:comments width:commentlb.width font:commentlb.font].height;
    commentlb.text = comments;
    [view addSubview:commentlb];
    
    
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return WFCGFloatY(53 + 132.5);
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
